import {
    Bus,
    CHART_CHAIN_SUBSCRIBE,
    CHART_KLINE_COMPLETE,
    CHART_KLINE_GOON,
    CHART_KLINE_SUBSCRIBE,
    CHART_KLINE_SUBSCRIBE_TIME,
    CHART_KLINE_UNSUBSCRIBE,
    CLOSE,
    CONN_ACTIVITY_RETRY,
    CONN_IMPORTANT,
    CONN_RETRY,
    CONN_STATUS,
    TOGGLE_PAUSE
} from "../connetion/Bus";
import { DayBroadcast } from "./DayBroadcast";
import { IBroadcast } from "./IBroadcast";
import { KlineBroadcast } from "./KlineBroadcast";
// import { PingBroadcast } from "./PingBroadcast";
// import { ActivityBroadcast } from "./ActivityBroadcast";
import { RankBroadcast } from "./RankBroadcast";
import { CleverBroadcast } from "./CleverBroadcast";
import { SignalBroadcast } from "./SignalBroadcast";
import { URL_SOCKET, URL_SOCKET_CHAIN } from "@/comm/api";
import { ActivityChainBroadcast } from "./ActivityChainBroadcast";
// import { PingChainBroadcast } from "./PingChainBroadcast";
import { BaseSocket } from "./BaseSocket";
// import { ImportantBroadcast } from "./ImportantBroadcast";

export class CoreConn {
    static _instance: CoreConn;

    socket: WebSocket = null as any;
    chainSocket: WebSocket = null as any;
    // url = "ws://192.168.150.188:8088/websocket";
    broadcasts: IBroadcast[] = [];
    mainMap: Map<string, IBroadcast> = new Map();
    subMap: Map<string, IBroadcast> = new Map();

    static KEY_KLINE = "KLINE";
    static KEY_DAY = "TICKER";
    static KEY_PING = "pong";
    static KEY_ACTIVITY = "ON_CHAIN";
    static KEY_RANK = "COINS";
    static KEY_CLEVER = "CLEVER";
    static KEY_SIGNAL = "SIGNAL";

    // static KEY_IMPORTANT = "IMPORTANT"

    temp: any = null;
    timer = 0;
    retryTimes = 0;
    pause = false;
    isReady = false;

    maxAttempts = 5;
    currentAttempt = 0;
    lastTime = "";

    curSymbolName = "";
    curIntervalName = "";
    curAddress = "";

    mainSocket: BaseSocket;
    subSocket: BaseSocket;

    constructor() {
        this.mainSocket = new BaseSocket(URL_SOCKET);
        this.subSocket = new BaseSocket(URL_SOCKET_CHAIN);

        this.mainSocket.onOpen = () => {
            (this.mainMap.get(CoreConn.KEY_DAY) as DayBroadcast).changeParam(this.curSymbolName);
            (this.mainMap.get(CoreConn.KEY_SIGNAL) as SignalBroadcast).changeParam(this.curSymbolName, this.curIntervalName);
            (this.mainMap.get(CoreConn.KEY_KLINE) as KlineBroadcast).changeParam(this.curSymbolName, this.curIntervalName);
            this.mainMap.get(CoreConn.KEY_RANK)!.subscribe();
        };

        this.subSocket.onOpen = () => {
            (this.subMap.get(CoreConn.KEY_ACTIVITY) as ActivityChainBroadcast)!.changeParam(this.curSymbolName);
        };

        this.mainSocket.onMessage = (jsondata: any) => {
            const broadcast = this.mainMap.get(jsondata.topic);
            if (broadcast) {
                broadcast.handle(jsondata);
            }
        };

        this.subSocket.onMessage = (jsondata: any) => {
            const broadcast = this.subMap.get(jsondata.topic);
            if (broadcast) {
                broadcast.handle(jsondata);
            }
        };

        // Bus.on(CHART_KLINE_SUBSCRIBE, (e: any) => {
        //     console.log("jsz", "CHART_KLINE_SUBSCRIBE");
        //     this.temp = e;
        //     this.init(e);
        // });

        // Bus.on(CHART_KLINE_COMPLETE, (e: any) => {
        //     console.log("jsz", "CHART_KLINE_COMPLETE");
        //     this.temp = e;
        //     this.init(e);
        // });

        // Bus.on(CHART_CHAIN_SUBSCRIBE, (e: any) => {
        //     (this.subMap.get(CoreConn.KEY_ACTIVITY) as ActivityChainBroadcast).changeParam(e);
        // });

        // Bus.on(CHART_KLINE_UNSUBSCRIBE, () => {
        //     this.cancelKline();
        // });

        // Bus.on(CHART_KLINE_GOON, e => {
        //     console.log("参数 e", e);
        //     this.init(e);
        // });

        // Bus.on(CHART_KLINE_SUBSCRIBE_TIME, (e: any) => {
        //     console.log("jsz", "change time");
        //     this.lastTime = e;
        //     (this.mainMap.get(CoreConn.KEY_KLINE) as KlineBroadcast).changeInterval(e);
        // });

        // Bus.on(CONN_RETRY, () => {
        //     this.retryTimes = 0;
        //     this.start();
        // });

        Bus.on(TOGGLE_PAUSE, (e: any) => {
            this.mainSocket.togglePause();
            this.subSocket.togglePause();
            alert("socket stop");
        });

        // Bus.on(CONN_ACTIVITY_RETRY, (e: any) => {
        //     console.log("aaaa=====================", "CONN_ACTIVITY_RETRY");
        //     this.subMap.get(CoreConn.KEY_ACTIVITY)!.subscribe();
        // });

        this.mainMap = new Map();
        this.mainMap.set(CoreConn.KEY_KLINE, new KlineBroadcast());
        this.mainMap.set(CoreConn.KEY_DAY, new DayBroadcast());
        this.mainMap.set(CoreConn.KEY_RANK, new RankBroadcast());
        this.mainMap.set(CoreConn.KEY_CLEVER, new CleverBroadcast());
        this.mainMap.set(CoreConn.KEY_SIGNAL, new SignalBroadcast());

        this.subMap = new Map();
        this.subMap.set(CoreConn.KEY_ACTIVITY, new ActivityChainBroadcast());
    }

    changeCoin(coin: string) {
        (this.mainMap.get(CoreConn.KEY_KLINE) as any).changeCoin(coin);
        (this.mainMap.get(CoreConn.KEY_DAY) as any).changeCoin(coin);
    }

    init(e: any) {
        (this.mainMap.get(CoreConn.KEY_KLINE) as any).changeParam(e.symbol, e.interval);
        (this.mainMap.get(CoreConn.KEY_DAY) as any).changeParam(e.symbol);
        (this.mainMap.get(CoreConn.KEY_SIGNAL) as any).changeParam(e.symbol);

        // (this.subMap.get(Conn.KEY_ACTIVITY) as ActivityChainBroadcast).changeParam(e.symbol);
    }

    addChannel(broadcast: IBroadcast) {
        this.broadcasts.push(broadcast);
    }

    cancelKline() {
        (this.mainMap.get(CoreConn.KEY_KLINE) as any).unSubscribe();
    }

    start() {
        return new Promise(resolve => {
            this.mainSocket.init();
            this.subSocket.init();
            resolve(true);
        });
    }

    static get ins(): CoreConn {
        if (!CoreConn._instance) {
            CoreConn._instance = new CoreConn();
        }
        return CoreConn._instance;
    }
}
